﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RoketPack.Management.Objects
{
    class Variable
    {
        private string p_Name = null;
        private string p_Value = null;
        private List<Variable> p_Variables = null;
        private bool p_PreviouslyUsed = false;

        internal Variable(string name, string value)
        {
            this.p_Name = name;
            this.p_Value = value;
        }

        internal string Name
        {
            get
            {
                return this.p_Name;
            }
        }

        internal string Value
        {
            get
            {
                if (this.p_PreviouslyUsed)
                    throw new RecursiveVariableDependencyFoundException();

                this.p_PreviouslyUsed = true;

                // Build a list of variables that excludes this one.
                List<Variable> exc = new List<Variable>();
                foreach (Variable v in this.p_Variables)
                    if (v != this && v.Name != this.p_Name)
                        exc.Add(v);

                string res = PathTranslator.Parse(this.p_Value, exc);

                this.p_PreviouslyUsed = false;

                return this.p_Value;
            }
        }

        internal List<Variable> Variables
        {
            set
            {
                this.p_Variables = value;
            }
        }
    }

    public class RecursiveVariableDependencyFoundException : ApplicationException
    {
        public RecursiveVariableDependencyFoundException() : base() { }
        public RecursiveVariableDependencyFoundException(string message) : base(message) { }
        public RecursiveVariableDependencyFoundException(string message, System.Exception inner) : base(message, inner) { }
    }
}
